package com.xq.controller;

import com.xq.pojo.Teacher;
import com.xq.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author xq
 * @version 1.0
 * @date 2021/4/16 19:17
 * @description 跳转页面的控制器
 */
@Controller
@RequestMapping("index")
public class IndexController {

    /**
     * 跳转到hello页面
     * @return
     */
    @RequestMapping("hello")
    public String hello() {
        return "hello";
    }

    /**
     * 跳转到用户管理页面
     * @return
     */
    @RequestMapping("userManager")
    public String userManager() {
        return "user/userManager";
    }

    /**
     * 跳转到学生信息
     * @return
     */
    @RequestMapping("showStudent")
    public String showStudent() {
        return "showStudent";
    }

    /**
     * 跳转到教师信息
     * @return
     */
    @RequestMapping("showTeacher")
    public String showTeacher(Model model) {
        Teacher teacher = new Teacher(01, "小娜", "女", 23, "1722891209", new Date(), 6000.00);
        model.addAttribute("teacher",teacher);
        return "showTeacher";
    }

    /**
     * 跳转到教师信息列表
     * @param model
     * @return
     */
    @RequestMapping("showStudentList")
    public String showStudentList(Model model) {
        List<Teacher> teacherList = new ArrayList<>();
        for (int i = 1; i <=10 ; i++) {
            teacherList.add(new Teacher(0+i,"小艾"+i,"女",18+i,"1722891209",new Date(), 5000.00+i*100));
        }
        model.addAttribute("teacherList",teacherList);
        return "teacherList";
    }


    /**
     * 三大作用域 request session context 分别给这些作用域设值
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("showObjects")
    public String showObjects(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        ServletContext context = request.getServletContext();

        model.addAttribute("name","name_model");
        request.setAttribute("name", "name_request");
        session.setAttribute("name", "name_session");
        context.setAttribute("name", "name_context");
        return "showObjects";
    }


    /**
     * 使用注解来格式化日期
     * @param date
     * @return
     */
    @RequestMapping("showDate01")
    @ResponseBody
    public Map<String, Object> showDate01(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        Map<String, Object> map = new HashMap<>(12);
        System.out.println(date.toLocaleString());
        map.put("msg","success");
        map.put("date",date);
        return map;
    }

    /**
     * 使用全局的配置格式化日期
     * @param date
     * @return
     */
    @RequestMapping("showDate02")
    @ResponseBody
    public Map<String, Object> showDate02(Date date) {
        Map<String, Object> map = new HashMap<>(12);
        System.out.println(date.toLocaleString());
        map.put("msg","success");
        map.put("date",date);
        return map;
    }

    /**
     * 返回用户数据
     * @return
     */
    @RequestMapping("getUser")
    @ResponseBody
    public User getUser() {
        return new User(1,"小明",new Date());
    }
}
